<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class party_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		
		
	public function get_head(){
			$q = $this->db->get('head');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}	

	public function get_parties1($id_head){
		
	 $query = $this->db->get_where('users',array('id_head'=>$id_head));	
	 $parties = $query->result_array();//	 print_r($parties);exit;
	 foreach($parties as $party){
	 	
		$this->db->select("id_user,user_name,SUM(credit_amount)as credit,SUM(debit_amount)as debit");
	 	$this->db->from("accounts");
		$this->db->where('id_user = ', $party['id']);
		$query1=$this->db->get();
		print("<pre>");
		print_r($query1->row_array());
	/* $query1 = $this->db->get_where('accounts',array('id_user'=>$party['id']));	
	 $accounts = $query1->result_array();
	*/// echo $party['id'];exit;
	/*print("<pre>");
	 print_r($accounts);exit;
	*/ 
	 }
	 exit;
	 	
	
	}
	
	
	
	
	
	
	public function get_customer($id_head){
		if($id_head == 1){
				$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit");
				$this->db->from('customer', 'accounts');
				$this->db->join('accounts', 'customer.id = accounts.id_user' );
				$this->db->where('accounts.type',1);
				$this->db->group_by('accounts.id_user');  
				$query1=$this->db->get();
				return $query1;                                                                  
			
		}else if($id_head == 2){
				$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit");
				$this->db->from('supplier', 'accounts');
				$this->db->join('accounts', 'supplier.id = accounts.id_user' );
				$this->db->where('accounts.type',2);
				$this->db->group_by('accounts.id_user');  
				$query1=$this->db->get();
				return $query1;                                                                  
		}
		
	
	}
	
	
	public function get_supplier($id_head){
		if($id_head == 1){
				$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit");
				$this->db->from('customer', 'accounts');
				$this->db->join('accounts', 'customer.id = accounts.id_user' );
				$this->db->where('accounts.type',2);
				$this->db->group_by('accounts.id_user');  
				$query1=$this->db->get();
				return $query1;                                                                  
			
		}else if($id_head == 2){
				$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit");
				$this->db->from('supplier', 'accounts');
				$this->db->join('accounts', 'supplier.id = accounts.id_user' );
				$this->db->where('accounts.type',2);
				$this->db->group_by('accounts.id_user');  
				$query1=$this->db->get();
				return $query1;                                                                  
		}
		
	
	}
	
	
	
	
	
	
	public function uniquecustomer($id_user){
		
		 $query1 = $this->db->get_where('accounts',array('id_user'=>$id_user,'type'=>'1'));	
		 //print_r($query1->row());exit;
		 return $query1;
	
	}	
	
	public function getcustomer_deatils($id_user){
	
		$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit, customer.tin_no as tin_no,customer.email as user_email,customer.address as user_address ");
		$this->db->from('customer', 'accounts');
		$this->db->join('accounts', 'customer.id = accounts.id_user' );
		$this->db->where('accounts.id_user =',$id_user);
		$this->db->where('accounts.type =','1');
		$query1=$this->db->get();
		//print_r($query1->row());exit;
		return($query1->row());
	}
	

		public function uniquesupplier($id_user){
		
		 $query1 = $this->db->get_where('accounts',array('id_user'=>$id_user,'type'=>'2'));
		 return $query1;
	
	}	
	
	public function getsupplier_deatils($id_user){
	
		$this->db->select("accounts.id_user,accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit, supplier.tin_no as tin_no,supplier.email as user_email,supplier.address as user_address ");
		$this->db->from('supplier', 'accounts');
		$this->db->join('accounts', 'supplier.id = accounts.id_user' );
		$this->db->where('accounts.id_user =',$id_user);
		$this->db->where('accounts.type =','2');
		$query1=$this->db->get();
		return($query1->row());
		
	
/*	
		$this->db->select("accounts.user_name,SUM(accounts.debit_amount)as debit ,SUM(accounts.credit_amount)as credit");
		$this->db->from('accounts');
		$this->db->where('id_user =',$id_user);
		$query1=$this->db->get();
		return $query1; 
		
*/		
	}


	


}
